/*
	File:		Laser.cpp
	Course:		SGD 1301
	Author:		Peter Nguyen
	Purpose:	CLaser class handles the laser entity
*/
#include "Laser.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../SGD Wrappers/CSGD_MessageSystem.h"
#include "DestroyEntityMessage.h"
#include "Game.h"
#include "Ship.h"
CLaser::CLaser()
{
	m_pOwner = nullptr;
	m_eType = ENT_LASER;
}

CLaser::~CLaser()
{
	m_pOwner->Release();
	m_pOwner = nullptr;
}

void CLaser::Update( float fElapsedTime )
{
	CEntity::Update(fElapsedTime);
	if(OutsideBound())
		CSGD_MessageSystem::GetInstance()->SendMsg(new CDestroyEntityMessage(this));
}

bool CLaser::OutsideBound()
{
	CGame* gamePtr = CGame::GetInstance();
	if(GetPosX()+GetWidth()*0.5f<0)
		return true;
	else if(GetPosX()>gamePtr->GetWorldWidth())
		return true;
	if(GetPosY()+GetHeight()*0.5f<0)
		return true;
	else if(GetPosY()>gamePtr->GetWorldHeight())
		return true;
	return false;
}

void CLaser::Render( void )
{
	CGame* gamePtr = CGame::GetInstance();
	
	POINT start = {(int)GetPosX()-gamePtr->GetCameraX(),(int)GetPosY()-gamePtr->GetCameraY()};
	rotate_point(gamePtr->GetWidth()/2.0f,gamePtr->GetHeight()/2.0f,gamePtr->GetCameraRotation(),start);
		
	if(CGame::DEBUG_MODE)
	{
		//line from bullet to owner
		CSGD_Direct3D::GetInstance()->DrawLine(
			start.x,
			start.y,
			int(m_pOwner->GetPosX()-gamePtr->GetCameraX()),
			int(m_pOwner->GetPosY()-gamePtr->GetCameraY()),
			255,255,255);
	}

	RECT srcRECT = {0,0,GetWidth(),GetHeight()};

	CSGD_TextureManager::GetInstance()->Draw( GetImageID(),
		start.x-GetWidth()/2, start.y-GetHeight()/2,
		1.0f,1.0f,&srcRECT, 
		GetWidth() *0.5f,GetHeight()*0.5f,m_fRotation+gamePtr->GetCameraRotation());
	if(CGame::DEBUG_MODE)
	{
		RECT collisonRECT = {(LONG)(start.x-GetWidth()*0.5f),(LONG)(start.y-GetHeight()*0.5f),(LONG)(start.x+GetWidth()*0.5f),(LONG)(start.y+GetHeight()*0.5f)};
		CSGD_Direct3D::GetInstance()->DrawRect(collisonRECT,255,0,0);
	}
}

RECT CLaser::GetRect( void ) const
{
	RECT rect = {LONG(GetPosX()-GetWidth()*0.1f),LONG(GetPosY()-GetHeight()*0.1f),
				 LONG(GetPosX()+GetWidth()*0.1f),LONG(GetPosY()+GetHeight()*0.1f)};
	return rect;
}

bool CLaser::CheckCollision( IEntity* pOther )
{
	if(m_pOwner==pOther)
		return false;
	RECT rSelf	= this->GetRect();
	RECT rOther	= pOther->GetRect();
	RECT rOverlap;

	if( IntersectRect( &rOverlap, &rSelf, &rOther ) == TRUE )
	{
		if((pOther->GetType()==ENT_AISHIP)||(pOther->GetType()==ENT_INPUTSHIP))
		{
			CShip* entPtr = (CShip*)pOther;
			float toOther = atan2(entPtr->GetPosY()-GetPosY(),entPtr->GetPosX()-GetPosX());
			if(entPtr->spinOutTimer<=0)
				entPtr->spinOutTimer=1.0f;
			entPtr->spinOutVector.fX = cos(toOther);
			entPtr->spinOutVector.fY = sin(toOther);
			CSGD_MessageSystem::GetInstance()->SendMsg(new CDestroyEntityMessage(this));
			if(m_pOwner->GetType()==ENT_INPUTSHIP)
				CGame::GetInstance()->ChangeScore(1337);
			return true;
		}
		return true;
	}
	return false;
}